package com.itheima.smspringboot.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.smspringboot.common.Result;
import com.itheima.smspringboot.entity.Admin;
import com.itheima.smspringboot.exception.CustomerExpception;
import com.itheima.smspringboot.service.AdminService;
import com.itheima.smspringboot.utils.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private final AdminService adminService;

    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }


    // 导入接口
    @PostMapping("/import")
    public Result importAdmins(@RequestParam("file") MultipartFile file) {
        // 1. 校验文件是否为空
        if (file.isEmpty()) {
            throw new CustomerExpception("400", "导入文件不能为空");
        }

        // 2. 校验文件类型
        String filename = file.getOriginalFilename();
        if (filename == null || (!filename.endsWith(".xlsx") && !filename.endsWith(".xls"))) {
            throw new CustomerExpception("400", "仅支持 .xlsx 或 .xls 格式文件");
        }

        try {
            // 直接传递 MultipartFile 到 ExcelUtil
            List<Admin> adminList = ExcelUtil.importExcel(file, Admin.class);
            adminService.importBatch(adminList);
            return Result.success("导入成功");
        } catch (Exception e) {
            throw new CustomerExpception("500", "导入失败: " + e.getMessage());
        }
    }

    @GetMapping("/export")
    public void exportAdmins(HttpServletResponse response) throws IOException {
        List<Admin> adminList = adminService.selectAllAdmin(new Admin());
        ExcelUtil.export(response, "管理员数据", "管理员列表", adminList, Admin.class);
    }


    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) {
        Admin dbadmin = adminService.login(admin);
        if ( dbadmin!= null && dbadmin.getPassword().equals(admin.getPassword())) {
            return Result.success(dbadmin);
       }else {
            throw new CustomerExpception("用户名或者密码错误");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {

        adminService.register(admin);

        return Result.success();


    }


    @PostMapping("/add")
    public Result add(@RequestBody Admin admin) {
       adminService.add(admin);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.updateById(admin);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();

    }

    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Admin> adminList) {
       adminService.deleteBatch(adminList);
        return Result.success();

    }


    @GetMapping("/selectAll")
    public Result selectAll(Admin admin) {
        List<Admin> adminList = adminService.selectAllAdmin(admin);
        return Result.success(adminList);
    }


    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             Admin admin){
        PageInfo pageInfo = adminService.selectPage(pageNum, pageSize,admin);
        return Result.success(pageInfo);
    }




}
